<?php
  class PageUtil{
  	var $pageUrl;//当前页面url，如果没有参数可以用?a=a结束，例如 abc.php?a=a
  	var $totalPage;//总页数
  	var $prePageIndex;
  	var $nextPageIndex;
  	var $currPageIndex;
  	var $firstPageIndex;
  	var $lastPageIndex;
  	var $totalRec;
  	var $pageSize;
  	var $sql;
  	var $pk;
  	var $desc;
  	function PageUtil($currPageIndex,$pageSize,$pageUrl,$sql,$pk,$desc){
            $this->currPageIndex = $currPageIndex;
            $this->pageSize = $pageSize;
            $this->pageUrl = $pageUrl;
            $this->sql = $sql;
            $this->pk = $pk;
            $this->desc = $desc;
            $this->init();
    }
    function init(){
    	$this->totalRec = $this->computeTotalRec();
    	$this->totalPage = floor($this->totalRec/$this->pageSize);
    	if ($this->totalRec % $this->pageSize > 0){
                $this->totalPage = $this->totalPage + 1;
    	}
    }
    function computeTotalRec(){
    	$query = " select count(*) c from";
    	$query.= " (";
    	$query.= " ".$this->sql."";
    	$query.= " )count_table";
    	$result = mysql_query($query);
    	$row = mysql_fetch_array($result);
    	return $row['c'];
    }
    function pageDataSet(){
    	    $n1 = ($this->currPageIndex- 1) * $this->pageSize ;
            $query = " select * from(";
            $query.= $this->sql;
            $query.= " )dt order by "; 
            $query.= "dt.".$this->pk;
            $query.= " ".$this->desc;
            $query.= " limit ".$n1.", ".$this->pageSize;
            $result = mysql_query($query);
            if(!$result){
            	 echo mysql_error();
            }
            return $result;            
    }
   function  createPageFooter()
        {
            $this->prePageIndex = $this->currPageIndex - 1;
            $this->nextPageIndex = $this->currPageIndex + 1;
            $str="";
            if ($this->currPageIndex > 1)
            {
                $str.="<a href='".$this->pageUrl."&page=1&pageSize=".$this->pageSize."'>首页</a>&nbsp;";
            }
            else
            {
                $str.="首页&nbsp;";
            }
            if ($this->currPageIndex > 1)
            {
                $str.="<a href='".$this->pageUrl."&page=".$this->prePageIndex."&pageSize=".$this->pageSize."'>上页</a>&nbsp;";
            }
            else
            {
                $str.="上页&nbsp;";
            }
            $str.="&nbsp;当前".$this->currPageIndex."页&nbsp;";
            if ($this->currPageIndex < $this->totalPage)
            {
                $str.="<a href='".$this->pageUrl."&page=".$this->nextPageIndex."&pageSize=".$this->pageSize."'>下页</a>&nbsp;";
            }
            else
            {
                 $str.="下页";
            }
            if ($this->totalPage > 1 && $this->currPageIndex != $this->totalPage)
            {
                 $str.="<a href='".$this->pageUrl."&page=".$this->totalPage."&pageSize=".$this->pageSize."'>末页</a>&nbsp;&nbsp;";
            }
            else
            {
                $str.="末页";
            }
            $str.=" 共".$this->totalRec."条记录";
            $str.="  每页<SELECT size=1 name=pagesize onchange='window.location.href=this.value'>";
            
            if ($this->pageSize == 3)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=3 selected>3</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=3>3</OPTION>";
            }

            if ($this->pageSize == 10)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=10 selected>10</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=10>10</OPTION>";
            }
            if ($this->pageSize == 20)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=20 selected>20</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=20>20</OPTION>";
            }
            if ($this->pageSize == 50)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=50 selected>50</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=50>50</OPTION>";
            }
            if ($this->pageSize == 100)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=100 selected>100</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=100>100</OPTION>";
            }
            $str.="</SELECT>";
            $str.="条 分".$this->totalPage."页显示 转到";
            $str.="<SELECT size=1 name=Pagelist onchange='window.location.href=this.value'>";
            for ($i = 1; $i < $this->totalPage + 1; $i++)
            {
                if ($i == $this->currPageIndex)
                {
                    $str.="<OPTION value=".$this->pageUrl."&page=".$i."&pageSize=".$this->pageSize." selected>".$i."</OPTION>";
                }         
                else         
                {
                    $str.="<OPTION value=".$this->pageUrl."&page=".$i."&pageSize=".$this->pageSize.">".$i."</OPTION>";
                }
            }
            $str.="</SELECT>页";
     
            return $str;
        }
  }
?>